/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sadhar.sipp.laporanrabunit;

//////////
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.data.JRTableModelDataSource;
import org.sadhar.sia.common.ClassConnection;
import org.sadhar.sia.common.ClassSession;
import org.sadhar.sia.framework.ClassApplicationModule;
import org.sadhar.sipp.laporananggaranpendapatanuangformulirmahasiswabaru.ResultSetTableModel;
import org.sadhar.sipp.unkerja.UnKerja;
import org.sadhar.sipp.unkerja.UnKerjaDAO;
import org.sadhar.sipp.unkerja.UnKerjaDAOImpl;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zkex.zul.Jasperreport;
import org.zkoss.zul.Button;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Panel;
import org.zkoss.zul.Row;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 *
 * @author Yohan Hardinugroho
 */
public class LaporanRABUnitWnd extends ClassApplicationModule {

    private Label lblTahunAnggaran;
    private Label lblUnitKerja;
    private Button buttonExport;
    private Button buttonHideExport;
    private Jasperreport report;
    private ClassSession session;
    private Panel panelReport;
    private Listbox lstboxformat;
    private LaporanRABUnitDAO laporanRABUnitDAO;
    private UnKerjaDAO unKerjaDAO;
    private Row rowUnitForUnit;
    private Row rowPilihUnit;
    private Row rowUnitForBK;
    private Textbox txtboxKeySearch;
    private Button btnCari;
    private Textbox txtboxKd_unit_kerja;
    private Listbox lstboxJenisRAB;
    private Label lblUnitKerja2;
    private boolean keuangan = false;

    public LaporanRABUnitWnd() {
        session = new ClassSession("2009","08000000");// "02001000"
        laporanRABUnitDAO = new LaporanRABUnitDAOImpl();
        unKerjaDAO = new UnKerjaDAOImpl();
    }

    public void onCreate() throws Exception {
        panelReport = (Panel) getFellow("panelReport");
        lblTahunAnggaran = (Label) getFellow("lblTahunAnggaran");
        lblUnitKerja = (Label) getFellow("lblUnitKerja");
        buttonExport = (Button) getFellow("buttonExport");
        buttonHideExport = (Button) getFellow("buttonHideExport");
        report = (Jasperreport) getFellow("report");
        lstboxformat = (Listbox) getFellow("lstboxformat");
        rowUnitForUnit = (Row) getFellow("rowUnitForUnit");
        rowPilihUnit = (Row) getFellow("rowPilihUnit");
        rowUnitForBK = (Row) getFellow("rowUnitForBK");
        txtboxKeySearch = (Textbox) getFellow("txtboxKeySearch");
        btnCari = (Button) getFellow("btnCari");
        txtboxKd_unit_kerja = (Textbox) getFellow("txtboxKd_unit_kerja");
        lblUnitKerja2 = (Label) getFellow("lblUnitKerja2");
        lstboxJenisRAB = (Listbox) getFellow("lstboxJenisRAB");


        lblTahunAnggaran.setValue(session.getThnAnggaran());
        UnKerja uk = unKerjaDAO.select(session.getKodeUnit());
        lblUnitKerja.setValue(uk.getNamaUnitKerja());
        load();

    }

    public void load() {
        String kodeUnit = session.getKodeUnit();
        int compare = kodeUnit.compareTo("08000000");

        if (compare == 0) {
            rowUnitForBK.setVisible(true);
            rowPilihUnit.setVisible(true);
            rowUnitForUnit.setVisible(false);
            keuangan = true;

        } else {
            rowUnitForBK.setVisible(false);
            rowPilihUnit.setVisible(false);
            rowUnitForUnit.setVisible(true);
            keuangan = false;
        }


    }

    public void export() throws Exception {
        String sql = "";
        report.setType(lstboxformat.getSelectedItem().getValue().toString());

        String tahunKelompok = laporanRABUnitDAO.tahunKelompokAnggaranYangDigunakan(session.getThnAnggaran());
        if (keuangan) {

            int i = lstboxJenisRAB.getSelectedIndex();
            boolean emptyornot = txtboxKeySearch.getValue().trim().isEmpty();
            if (i == 0) {
                report.setSrc("reports/laporanrabunit/LaporanRABUnit.jasper");
                if (emptyornot) {
                    sql = laporanRABUnitDAO.getQueryBiroKeuanganNonRapat(session.getThnAnggaran(), tahunKelompok);
                } else {
                    sql = laporanRABUnitDAO.getQueryBiroKeuanganNonRapat(session.getThnAnggaran(), tahunKelompok, txtboxKd_unit_kerja.getValue());
                }

            } else if (i == 1) {
                report.setSrc("reports/laporanrabunit/LaporanRABUnitRapatKerja.jasper");
                if (emptyornot) {
                    sql = laporanRABUnitDAO.getQueryBiroKeuanganRapat(session.getThnAnggaran(), tahunKelompok);
                } else {
                    sql = laporanRABUnitDAO.getQueryBiroKeuanganRapat(session.getThnAnggaran(), tahunKelompok, txtboxKd_unit_kerja.getValue());
                }
            } else if (i == 2) {
                if (emptyornot) {
                    report.setSrc("reports/laporanrabunit/LaporanAnggaranBiayaGaji.jasper");
                    sql = laporanRABUnitDAO.getQueryBiayaGaji(session.getThnAnggaran());
                } else {
                    report.setSrc("reports/laporanrabunit/LaporanAnggaranBiayaGaji.jasper");
                    sql = laporanRABUnitDAO.getQueryBiayaGaji(session.getThnAnggaran(), txtboxKd_unit_kerja.getValue());
                }
            } else {
            }
        } else {

            int i = lstboxJenisRAB.getSelectedIndex();
            if (i == 0) {
                report.setSrc("reports/laporanrabunit/LaporanRABUnit.jasper");
                sql = laporanRABUnitDAO.getQueryUnitNonRapat(session.getThnAnggaran(), tahunKelompok, session.getKodeUnit());
            } else if (i == 1) {
                report.setSrc("reports/laporanrabunit/LaporanRABUnitRapatKerja.jasper");
                sql = laporanRABUnitDAO.getQueryUnitRapat(session.getThnAnggaran(), tahunKelompok, session.getKodeUnit());
            } else if (i == 2) {
                report.setSrc("reports/laporanrabunit/LaporanAnggaranBiayaGaji.jasper");
                sql = laporanRABUnitDAO.getQueryBiayaGaji(session.getThnAnggaran());
            }
        }
        Map m = new HashMap();
        m.put("tahunAnggaran", session.getThnAnggaran());
        report.setParameters(m);
        ResultSetTableModel rstm = new ResultSetTableModel(ClassConnection.getJdbc().getDataSource().getConnection(), sql);

        report.setDatasource(new JRTableModelDataSource(rstm));
        showExport();
    }

    public void showExport() {
        panelReport.setVisible(true);
        panelReport.setOpen(true);
        buttonHideExport.setVisible(true);
    }

    public void hideExport() {
        panelReport.setVisible(false);
        buttonHideExport.setVisible(false);
    }

    public void btnCariOnClick() throws Exception {
        final Window win = (Window) Executions.createComponents(
                "sippzul/laporanrabunit/UnitKerjaPopup.zul", this,
                null);
        Textbox txtboxSearch = (Textbox) win.getFellow("txtboxSearch");
        txtboxSearch.setValue(txtboxKeySearch.getValue());
        win.doModal();

        txtBoxKdUnitKerjaOnChange();
    }

    public void txtBoxKdUnitKerjaOnChange() {
        lblUnitKerja.setValue(lblUnitKerja2.getValue());
        if (txtboxKeySearch.getValue().trim().isEmpty()) {
            txtboxKd_unit_kerja.setValue("");
        }
    }
}
